package com.example.home.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.example.home.R
import com.example.home.databinding.ItemVideoBinding
import com.example.home.model.entity.VideoEntity

class VideoAdapter: RecyclerView.Adapter<VideoAdapter.VideoHolder>() {

    private var dataSource:MutableList<VideoEntity>  = mutableListOf()
    private lateinit var context:Context

    /**
     * 刷新数据
     */
    fun refresh(_dataSource:List<VideoEntity>,isClear:Boolean = false){
        if (isClear){
            this.dataSource.clear()
        }
        this.dataSource.addAll(_dataSource)
        this.notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoHolder {
        this.context = parent.context
        val view = LayoutInflater.from(context).inflate(R.layout.item_video,parent,false)
        return VideoHolder(view)
    }

    override fun getItemCount(): Int {
        return this.dataSource.size
    }

    override fun onBindViewHolder(holder: VideoHolder, position: Int) {
        val video = this.dataSource.get(position)
        Glide.with(this.context).load(video.videomainimag).into(holder.subBinding.ivHomeHomeItemPrimary)
        holder.subBinding.tvHomeHomeItemTitle.text = video.title
    }

    class VideoHolder(view: View):RecyclerView.ViewHolder(view){
        val subBinding:ItemVideoBinding by lazy {
            ItemVideoBinding.bind(view)
        }
    }
}